package com.whiteape.book.ui.fragment;

import android.view.Menu;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hwangjr.rxbus.RxBus;
import com.whiteape.book.R;
import com.whiteape.book.common.RxEvent;
import com.whiteape.book.contract.WebSourceContract;
import com.whiteape.book.core.manager.SiteSourceManager;
import com.whiteape.book.model.dto.SiteSourceBean;
import com.whiteape.book.presenter.WebSourcePresenter;
import com.whiteape.book.ui.adapter.SiteSourceAdapter;
import com.whiteape.book.ui.base.BaseRVMVPFragment;
import com.whiteape.book.utils.RxUtils;
import com.whiteape.book.utils.StringUtils;
import com.whiteape.book.utils.ToastUtils;
import com.whiteape.book.widget.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SiteSourceContentFragment extends BaseRVMVPFragment<WebSourceContract.Presenter, SiteSourceBean> implements WebSourceContract.View {

    private List<SiteSourceBean> mData = new ArrayList<>();
    private SiteSourceAdapter mSiteSourceAdapter;

    @BindView(R.id.refresh_layout_loading)
    RefreshLayout refreshLayout;
    @Override
    protected void startLogic() {
        super.startLogic();
        mSmartRefreshLayout.setEnableRefresh(false);
        refreshLayout.showLoading();
        mPresenter.refreshWebSource();
    }

    /**
     * 过滤查询网源
     * @param word
     */
    public void filterByWord(String word) {

        if (StringUtils.isEmpty(word)) {
            mPresenter.refreshWebSource();
            return;
        }
        refreshLayout.showLoading();
        addDisposable(SiteSourceManager.getInstance().filterSiteSourceAllInRx(word)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(siteSourceBeanList -> {
                    mData.clear();
                    mData.addAll(SiteSourceManager.getInstance().filterSiteSourceAll(word));
                    refresh();
                    refreshLayout.showFinish();
                }));

    }

    @Override
    protected int getTotalStringTemplate() {
        return R.string.source_total_template;
    }

    @Override
    public void finishRefresh(List<SiteSourceBean> siteSourceBeanList) {
        mData.clear();
        mData.addAll(siteSourceBeanList);
        refreshLayout.showFinish();
        refresh();
    }

    @Override
    public void finishUpdate() {

    }

    @Override
    public void showErrorTip(String error) {

    }

    @Override
    protected WebSourceContract.Presenter bindPresenter() {
        return new WebSourcePresenter();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

    @Override
    protected boolean setBatchDeleteEnabled() {
        return true;
    }

    @Override
    protected boolean setAdapterModeSwitchEnabled() {
        return false;
    }

    @Override
    protected boolean initListOrGrid() {
        return true;
    }

    @Override
    protected List<BaseQuickAdapter<SiteSourceBean, BaseViewHolder>> setAdapter() {
        List<BaseQuickAdapter<SiteSourceBean, BaseViewHolder>> adapterList = new ArrayList<>();
        mSiteSourceAdapter = new SiteSourceAdapter(R.layout.item_site_source_list, mData);
        mSiteSourceAdapter.setSelectedList(mSelectedList);
        adapterList.add(mSiteSourceAdapter);
        return adapterList;
    }

    @Override
    protected void deleteItems(List<SiteSourceBean> itemList) {
        if (getContext() == null) {
            ToastUtils.show("程序可能出了点小问题，请重新打开操作");
            return;
        }
        new AlertDialog.Builder(getContext())
                .setTitle(R.string.wa_common_tip)
                .setMessage(R.string.delete_site_source_msg)
                .setPositiveButton(R.string.wa_common_sure,((dialogInterface, i) -> {
                    dialogInterface.dismiss();
                    addDisposable(SiteSourceManager.getInstance().deleteSiteSourceInRx(itemList)
                            .compose(RxUtils::toSimpleSingle)
                            .subscribe(aBoolean -> {
                                RxBus.get().post(RxEvent.SITE_SOURCE_DELETED, itemList);
                                mData.removeAll(itemList);
                                refresh();
                                ToastUtils.show("成功删除" + itemList.size() + "个网源");
                            }, throwable -> {
                                new AlertDialog.Builder(getContext())
                                        .setTitle(R.string.wa_common_tip)
                                        .setMessage(throwable.getMessage())
                                        .setPositiveButton(R.string.wa_common_know_it, (dialog, which) -> {
                                            dialog.dismiss();
                                        }).show();

                            }));
                }))
                .setNegativeButton(R.string.wa_common_cancel, ((dialogInterface, i) -> {
                    dialogInterface.dismiss();

                })).show();

    }

    @Override
    protected void onItemClickListener(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    protected void onItemChildClickListener(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    protected void onSelectStateChanged(boolean selectStateEnable) {

        if (mSiteSourceAdapter != null) {
            mSiteSourceAdapter.setSelectedStateEnable(selectStateEnable);
        }
    }

    @Override
    public void onPrepareOptionsMenu(@NonNull Menu menu) {
        super.onPrepareOptionsMenu(menu);
    }
}
